/*
 * @Descripttion: 
 * @Author: yubo
 * @Date: 2022-11-02 10:41:18
 * @LastEditTime: 2022-11-02 15:27:45
 */

/*
trait Add<RHS, Output> {
    fn my_add(self, rhs: RHS) -> Output;
}
impl Add<i32, i32> for i32 {
    fn my_add(self, rhs: i32) -> i32 {
        self + rhs
    }
}
impl Add<u32, i32> for u32 {
    fn my_add(self, rhs: u32) -> i32 {
        (self + rhs) as i32 
    }
}
fn main() {
    let (a,b,c,d) = (1i32, 2i32, 3u32, 4u32);
    let x: i32 = a.my_add(b);
    let y: i32 = c.my_add(d);
    assert_eq!(x, 3i32);
    assert_eq!(y, 7i32);
}
*/

pub trait Add<RHS = Self> {
    type Output;
    fn add(self, rhs: RHS) -> Self::Output; //关联类型
}
//Self 是每个 trait 都带有的隐式类型参数，代表实现当前trait的具体类型

impl Add for u32 {
    type Output = u32;
    fn add(self, other: u32) -> u32 {
        self + other
    }
}

//<&str>指明泛型类型为&str，并不是Self默认类型，这表明对于String类型字符串来说，加号右侧的值类似&str类型而不是String类型。
impl Add<&str> for String {
    type Output = String;
    fn add(mut self, other: &str) -> String {
        self.push_str(other);
        self
    }
}

fn main() {
    let a = "hello";
    let b = "world";
    let c = a.to_string() + b;
    println!("{:?}", c);
}